home *** CD-ROM | disk | FTP | other *** search
/ Fritz: All Fritz / All Fritz.zip / All Fritz / FILES / PROGNG_C / CUG187.LZH / R-JUST.C < prev    next >
C/C++ Source or Header  |  1985-12-31  |  1KB  |  39 lines

  1. /*@*****************************************************/
  2. /*@                                                    */
  3. /*@ r_just - right justify a string in a buffer of a   */
  4. /*@        given length. See c_just and l_just also.   */
  5. /*@                                                    */
  6. /*@   Usage:     r_just(string, size);                 */
  7. /*@       returns the address of the modified string.  */
  8. /*@       NOTE:  There must be size+1 character        */
  9. /*@              spaces avail in string.               */
  10. /*@                                                    */
  11. /*@*****************************************************/
  12.  
  13.  
  14. char *r_just(str, size)
  15. char *str;
  16. int size;
  17. {
  18.     char *s, *d;
  19.     int len, count;
  20.  
  21.     len = strlen(str);                /* get string length */
  22.  
  23.     if (len > size)                    /* truncate, if necessary */
  24.         str[size] = 0x00;
  25.     else if (len < size) {
  26.         d = str + size;                /* copy to leave room */
  27.         s = str + len;
  28.         count = len + 1;
  29.         while (count--)
  30.             *d-- = *s--;
  31.         count = size - len;            /* number of blanks to insert */
  32.         s = str;
  33.         while (count--)
  34.             *s++ = ' ';                /* add leading blanks */
  35.     }
  36.  
  37.     return str;
  38. }
  39.